package br.com.fiap.sas.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.com.fiap.sas.bean.AgenciaConta;
import br.com.fiap.sas.bean.Correntista;
import br.com.fiap.sas.factory.ConnectionFactory;

public class CorrentistaDAO {
	
	public static Correntista getCorrentistaDao(String numeroAgencia, String numeroConta) throws SQLException {

		AgenciaConta agenciaConta = AgenciaContaDAO.getAgenciaContaDao(numeroAgencia, numeroConta);

		Correntista correntista = null;
		Connection conn = ConnectionFactory.getConnection();

		if (agenciaConta == null) return correntista;
		
		String sql = "SELECT codigo, nome FROM correntista WHERE codigo = ?";
		
		PreparedStatement stmt = conn.prepareStatement(sql);
		stmt.setInt(1, agenciaConta.getCodigoCorrentista());
		
		ResultSet rs = stmt.executeQuery();

		if(rs.next()){
			correntista = new Correntista();
			correntista.setCodigo(rs.getInt("codigo"));
			correntista.setNome(rs.getString("nome"));
			correntista.setAgenciaConta(agenciaConta);
		}

		stmt.close();
		rs.close();
		conn.close();
		return correntista;
	}
}